package efr.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import efr.dao.AutorDao;
import efr.dao.impl.AutorDaoImpl;
import efr.entity.Autor;

/**
 * Servlet implementation class Op4AutorServlet
 */
public class Op4AutorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(Op4AutorServlet.class);
	
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Op4AutorServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		logger.debug("start doPost()");
		String nextPage = null;
		
		if ("Modifica".equals(request.getParameter("Modifica"))) {
			String id = request.getParameter("id");
			AutorDao autorDao = new AutorDaoImpl();
			Autor autor = autorDao.getAutor(Integer.valueOf(id));
			request.setAttribute("autor", autor);
			request.setAttribute("action", "Modifica");
			nextPage = "/formAutor.jsp";
		}
		if ("Sterge".equals(request.getParameter("Sterge"))) {
			String id = request.getParameter("id");
			AutorDao autorDao = new AutorDaoImpl();
			Autor autor = autorDao.getAutor(Integer.valueOf(id));
			autorDao.deleteAutor(autor);
			nextPage = "/ListAutori.do";
		}
		if ("Adauga".equals(request.getParameter("Adauga"))) {
			request.setAttribute("action", "Adauga");
			nextPage = "/formAutor.jsp";
		}

		RequestDispatcher view = request.getRequestDispatcher(nextPage);
		logger.debug("end doPost()");
		view.forward(request, response);
	}

}
